/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */
#ifndef PLSIGNAL0_H
#define PLSIGNAL0_H

#include <kernel/signalslot/lang/PLSignalBase.h>
#include <kernel/signalslot/lang/PLConnection0.h>
#include <kernel/signalslot/lang/PLISignalBase0.h>
#define HASHTABLE_WITHOUT_PLOBJECT
#include <tools/PLHashTable.h>

namespace PLSDK {

	/**
	 * Implement a signal with 0 parameters.
	 */
	class PLSignal0 : public PLSignalBase {
	public:
		/**
		 * Constructor.
		 */
		PLSignal0() : PLSignalBase(), m_index(0) {}
		/**
		 * Connect the signal.
		 *
		 * @param object The object to connect to.
		 * @param pmemfun The function to connect to.
		 * @return True if the connection was successfully.
		 */
		template<class desttype>
		plbool connect(desttype *object, void(desttype::*pmemfun)()) {
			PLConnection0<desttype> *connection = new PLConnection0<desttype>(object, pmemfun);
			PLSignalBase::connect(dynamic_cast<PLObject *>(object), dynamic_cast<PLConnectionBase *>(connection), PLSignalTypesSignal0Params);
			m_signalsPrivate.insert(m_index, connection);
			connection->setSlotID(m_index);
			m_index++;
			return true;
		}
		/**
		 * Emit the connections.
		 */
		void emit() {
			PLISignalBase0 *signal;
			PLHashTableEnumerator<pluint64, PLISignalBase0 *> en = m_signalsPrivate.valueEnumerator();
			while(en.hasNext()) {
				signal = en.next();
				signal->emit();
			}
		}
		/**
		 * Remove a connection from a signal with a id.
		 *
		 * @param slotID The slot id to remove.
		 */
		void removeSignal(pluint64 slotID) {
			PLISignalBase0 *data = m_signalsPrivate.remove(slotID);
			if(data != 0) {
				delete data;
				data = 0;
			}
		}
	private:
		PLHashTable<pluint64, PLISignalBase0 *> m_signalsPrivate;
		pluint64 m_index;
	};

}

#undef HASHTABLE_WITHOUT_PLOBJECT

#endif
